let log = console.log;

function noop() {
}

function isFunction(fn) {
    return typeof fn == 'function';
}

//window.onload
//document.onload
//document.documentElement.onload
function iframeReady(contentWindow, callback) {
    let iid;
    iid = setInterval(function () {
        if (contentWindow.document.readyState == 'complete') {
            clearInterval(iid);
            callback();
        }
    }, 1);
}

function documentReady(callback) {
    let iid;
    iid = setInterval(function () {
        if (document.readyState == 'complete') {
            clearInterval(iid);
            callback();
        }
    }, 1);
}

function dce(tagName) {
    return document.createElement(tagName);
}

function qs(node, selector) {
    return node.querySelector(selector);
}

function qsa(node, selector) {
    return node.querySelectorAll(selector);
}

//https://www.w3school.com.cn/jsref/dom_obj_event.asp
let dom_obj_event = {
    wheel: 'wheel',
    click: 'click',
    dblclick: 'dblclick',
    resize: 'resize'
};

function ae(node, type, fn, capture) {
    node.addEventListener(type, fn);
}

function re(node, type, fn, capture) {
    node.removeEventListener(type, fn);
}

function de(node, type) {
    node.dispatchEvent(new CustomEvent(type));
}

function addClass(node, clazz) {
    node.classList.add(clazz);
}

function removeClass(node, clazz) {
    node.classList.remove(clazz);
}

function hasClass(node, clazz) {
    return node.classList.contains(clazz);
}

function toggleClass(node, clazz) {
    if (hasClass(node, clazz)) {
        removeClass(node, clazz);
    } else {
        addClass(node, clazz);
    }
}

function setAttr(node, name, value) {
    node.setAttribute(name, value);
}

function getAttr(node, name) {
    return node.getAttribute(name);
}

function removeAttr(node, name) {
    return node.removeAttribute(name);
}

function show(node) {
    node.style.display = '';
}

function hide(node) {
    node.style.display = 'none';
}

function isHidden(node) {
    return node.style.display == 'none';
}

function isVisible(node) {
    return !isHidden(node);
}

function setBackgroundImage(node, src) {
    node.style.backgroundImage = 'url("' + src + '")';
}

function loadImage(src, callback) {
    let img = dce('img');
    img.onload = ()=> {
        let src = getAttr(img, 'src');
        let width = img.width;
        let height = img.height;
        callback(img, src, width, height);
    };
    img.src = src;
}

function setDimension(node, dimension) {
    node.style.width = dimension.width + 'px';
    node.style.height = dimension.height + 'px';
}

function setBackgroundPosition(node, position) {
    node.style.backgroundPositionX = position.x + 'px';
    node.style.backgroundPositionY = position.y + 'px';
}

function setBackgroundSize(node, width, height) {
    node.style.backgroundSize = width + 'px ' + height + 'px';
}

function setPositioning(node, position) {
    node.style.left = position.x + 'px';
    node.style.top = position.y + 'px';
}

function numberOfPixel(ps) {
    return Number(ps.substring(0, ps.length - 2));
}

//formatString("%5s% 03n",'Jack', 23);
//[ Jack 23]
function formatString() {
    log(arguments);
    if (arguments.length) {
    }
}

function apply_previousElementSibling() {
    //previousElementSibling
}

function newCSS() {
    let css = new CSSStyleSheet();
    css.insertRule("#blanc { color: white }", 0);
    css.deleteRule(0);
}

function newSimpleAnimation(node, keyframes, options, onfinish, oncancel, onremove) {
    let ao = node.animate(keyframes, options);//Animation
    if (isFunction(onfinish)) {
        ao.onfinish = onfinish;
    }
    if (isFunction(oncancel)) {
        ao.oncancel = oncancel;
    }
    if (isFunction(onremove)) {
        ao.onremove = onremove;
    }
    return {
        play: ()=>ao.play(),
        pause: ()=>ao.pause(),
        stop: ()=>ao.finish(),
        cancel: ()=>ao.cancel(),
        reverse: ()=>ao.reverse(),
        getAnimation: ()=>ao,
        getKeyframeEffect: ()=>ao.effect,
        setKeyframeEffect: (keyframes, options)=>ao.effect = new KeyframeEffect(node, keyframes, options),
        getKeyframes: ()=>ao.effect.getKeyframes(),
        setKeyframes: (keyframes)=>ao.effect.setKeyframes(keyframes)
    };
}

function getBeginOfMonth(date) {
    if (date == undefined) {
        date = new Date();
    }
    date.setDate(1);
    return getDateStr(date);
}

function getEndOfMonth(date) {
    if (date == undefined) {
        date = new Date();
    }
    date.setDate(1);
    date.setMonth(date.getMonth() + 1);
    date.setDate(0);
    return getDateStr(date);
}

function getDateStr(date) {
    if (date == undefined) {
        date = new Date();
    }
    let ny = date.getFullYear();
    let nm = date.getMonth() + 1;
    let nd = date.getDate();
    //TODO: replace with zeroPadding
    return [ny, nm > 9 ? nm : '0' + nm, nd > 9 ? nd : '0' + nd].join('-');
}

function getTimeStr(date) {
    if (date == undefined) {
        date = new Date();
    }
    let nh = date.getHours();
    let nm = date.getMinutes();
    let ns = date.getSeconds();
    let nms = date.getMilliseconds();
    //TODO: replace with zeroPadding
    return [nh > 9 ? nh : '0' + nh, nm > 9 ? nm : '0' + nm, ns > 9 ? ns : '0' + ns].join(':') + '.' + (nms > 99 ? nms : (nms > 9 ? '0' + nms : '00' + nms));
}

function datetime() {
    return getDateStr() + ' ' + getTimeStr();
}

//int 3 -> 0,1,2
function randomIndex(size) {
    return Math.floor(Math.random() * Math.pow(10, String(size).length)) % size;
}